﻿using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using System.Reflection;
using TcpJsonService.Core;
using TcpJsonService.Fvs;

namespace TcpJsonService
{
    internal class Program
    {
        static ILogger _logger;
        static void Main(string[] args)
        {
            AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
            var configuration = new ConfigurationBuilder()
           .AddJsonFile(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "appsettings.json"))
           .Build();
    
            using ILoggerFactory factory = LoggerFactory.Create(builder => builder.AddConsole());
            factory.AddProvider(new FileSizeLoggerProvider(configuration));
            ILogger logger = factory.CreateLogger("Program");

            logger.LogInformation($"system start {Assembly.GetExecutingAssembly().GetName().Version}");
            _logger = logger;
            int RouterPort = configuration.GetValue("Fvs:RouterPort", 7771);
            int NoticePort = configuration.GetValue("Fvs:NoticePort", 7772);
            int minNewPort = configuration.GetValue("Fvs:MinLampPort", 60000);
            int maxNewPort = configuration.GetValue("Fvs:MaxLampPort", 60000);
            int newMinNewPort = configuration.GetValue("Fvs:NewMinLampPort", 60001);
            int newMaxNewPort = configuration.GetValue("Fvs:NewMaxLampPort", 60001);

            Dictionary<int, FvsControllerChannel> channels = [];
            for (int i = minNewPort; i <= maxNewPort; i++)
            {
                FvsControllerChannel channel = new(i, logger);
                channels.Add(i, channel);
            }

            for (int i = newMinNewPort; i <= newMaxNewPort; i++)
            {
                FvsControllerChannel_New channel = new(i, logger);
                channels.Add(i, channel);
            }

            RouteChannel routeChannel = new(7771, logger, channels);
            NoticeChannel noticeChannel = new(7772, logger, channels);

            //VirtualFvsChannel fvsChannel = new(new IPEndPoint(IPAddress.Loopback, 7771), logger);
            //VirtualControllerChannel lampChannel = new VirtualControllerChannel(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 59980), logger, 100);
            //VirtualControllerChannel_New newLampChannel = new VirtualControllerChannel_New(new IPEndPoint(IPAddress.Parse("127.0.0.1"), 59970), logger, 2);

            //while (true)
            //{
            //    string str = Console.ReadLine();
            //    if (str == "exit")
            //    {
            //        break;
            //    }
            //    fvsChannel.Test(60000, 1);
            //    fvsChannel.Test(59960, 2);
            //    fvsChannel.Test1();
            //}

            while (true)
            {
                Thread.Sleep(1000 * 10);
            }

            //int controllerPort = configuration.GetValue("Json:ControllerPort", 10000);
            //logger.LogInformation($"controller port {controllerPort}");
            //ControllerChannel controllerChannel = new(controllerPort, logger, configuration);

            //List<int> jsonPorts = [];
            //configuration.Bind("Json:JsonPorts", jsonPorts);
            //List<JsonChannel> jsonChannels = [];
            //foreach (var jsonPort in jsonPorts)
            //{
            //    jsonChannels.Add(new JsonChannel(jsonPort, logger, controllerChannel));
            //}

            //List<int> controllerIds = [];
            //configuration.Bind("Json:ControllerIds", controllerIds);
            //LampChannel lampChannel = null;
            //if (controllerIds.Any(c => c == 77))
            //{
            //    lampChannel = new(new IPEndPoint(IPAddress.Loopback, controllerPort), logger, 77);
            //}

            //Console.ReadLine();

            //lampChannel?.Close();
            //foreach (var jsonChannel in jsonChannels)
            //{
            //    jsonChannel.Close();
            //}
            //controllerChannel.Close();
        }
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Exception ex = (Exception)e.ExceptionObject;
            _logger.LogError(ex, $"Runtime terminating: {e.IsTerminating}");
        }
    }
}
